<template>
  <div>
    <mbao-x item1="商品管理" item2="商品分类"></mbao-x>
    <el-card class="Top">
      <el-button type="primary" @click="OpenCate">添加分类</el-button>
      <!-- 树型表格 -->
      <el-table
        :data="tableData"
        class="Top"
        style="width: 100%; margin-bottom: 20px"
        row-key="cat_id"
        border
        :tree-props="props"
      >
        <el-table-column label="#" type="parentIndex" width="50px">
          <template slot-scope="scope">
            <span v-if="scope.row.isIndex">{{ scope.row.parentIndex }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="cat_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cat_deleted" label="是否有效">
          <template slot-scope="scope">
            <div>
              <i
                class="el-icon-success"
                style="color: green"
                v-if="scope.row.cat_deleted == false"
              ></i>
              <i
                class="el-icon-error"
                style="color: red"
                v-if="scope.row.cat_deleted == true"
              ></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="cat_level" label="排序">
          <template slot-scope="scope">
            <el-tag effect="dark" type="" v-if="scope.row.cat_level === 0">
              一级
            </el-tag>
            <el-tag
              effect="dark"
              type="success"
              v-if="scope.row.cat_level === 1"
            >
              二级
            </el-tag>
            <el-tag
              effect="dark"
              type="warning"
              v-if="scope.row.cat_level === 2"
            >
              三级
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="OpenEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="Del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加编辑分类对话框 -->
      <el-dialog :title="names" :visible.sync="dialogFormVisible">
        <el-form :model="form" :rules="rules" ref="form">
          <el-form-item label="父类名称" label-width="100px" prop="cat_name">
            <el-input v-model="form.cat_name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="父类分级"
            label-width="100px"
            prop="value"
            v-if="names == '添加商品分类'"
          >
            <el-cascader
              ref="csacadersVisible"
              v-model="value"
              :options="options"
              :props="arops"
              clearable
              @change="Close"
            ></el-cascader>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="ADDCate">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 分页 -->
      <el-pagination
        class="Top"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="forms.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="forms.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import MbaoX from '../components/MbaoX.vue'
export default {
  components: {
    MbaoX
  },
  data() {
    return {
      tableData: [],
      names: '',
      props: { children: 'children', hasChildren: 'hasChildren' },
      forms: {
        pagesize: 5,
        pagenum: 1
      },

      options: [],
      arops: {
        checkStrictly: true,
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      rules: {
        cat_name: [
          { required: true, message: '请输入分类名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        value: [{ required: true, message: '请选择父级分类', trigger: 'blur' }]
      },
      total: 0,
      form: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      value: [],
      dialogFormVisible: false,
      Id: 0
    }
  },

  created() {
    this.getlist()
  },

  mounted() {},
  methods: {
    async getlist() {
      const res = await this.$http.Cate(this.forms)
      console.log(res)
      this.tableData = res.result
      this.options = res.result
      this.total = res.total

      this.tableData.forEach((item, index) => {
        item.isIndex = true
        item.parentIndex = index + 1
      })
    },
    // 删除
    async Del(row) {
      let Dels
      try {
        Dels = await this.$confirm(
          '此操作将永久删除该文件, 是否继续?',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
      } catch (error) {
        Dels = error
      }
      if (Dels === 'confirm') {
        const res = this.$http.DELCate(row.cat_id)
        console.log(res)
        this.getlist()
      }
    },
    // 编辑
    OpenEdit(row) {
      this.names = '编辑商品分类'
      this.dialogFormVisible = true
      this.form = row
      this.Id = row.cat_id
      console.log(row)
    },
    async ADDCate() {
      if (this.names == '添加商品分类') {
        const res = await this.$http.ADDCates(this.form)
        console.log(res)
      } else {
        const res = await this.$http.EditCate(this.Id, this.form)
        console.log(res)
      }

      this.getlist()
      this.dialogFormVisible = false
    },
    Close() {
      console.log(this.value)
      if (this.value.length !== 0) {
        this.form.cat_level = this.value.length
        this.form.cat_pid = this.value[this.value.length - 1]
      } else {
        this.form.cat_pid = 0
        this.form.cat_level = 0
      }
      this.$refs.csacadersVisible.dropDownVisible = false
    },
    // 添加分类
    OpenCate() {
      this.dialogFormVisible = true
      this.names = '添加商品分类'
    },
    // 分页
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.forms.pagesize = val
      this.getlist()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.forms.pagenum = val
      this.getlist()
    }
  }
}
</script>
<style>
.radio {
  /* position: relative; */
}
.el-radio__input.is-checked .el-radio__inner {
  background-color: transparent;
}
.el-radio__inner::after {
  background-color: transparent;
}
.el-radio__inner {
  position: absolute;
  left: 6px;
  top: -14px;
  width: 72px;
  height: 25px;
  border: none;
  background: transparent;
}
</style>
<style scoped lang='scss'>
.Top {
  margin-top: 20px;
}
//
</style>
